<?php
	$departments = MDepartment::model()->findAll("is_active = TRUE order by id");
	$department_id = $department_id == "" ? $departments[0]->id : $department_id; 	
	$department_id = Yii::app()->request->getParam("department_id",$department_id);	
?>

<script>
	
	function reloadPage(){		
		$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val()},datatype:"json"}).trigger("reloadGrid",[{current:true}]);
		$("#tblData").jqGrid("setGridParam",{postData:{"karyawan_id" :$("#karyawan_id").val()},datatype:"json"}).trigger("reloadGrid",[{current:true}]);		
		$("#idSrcTblData").val("");
	}
	/*
	function newData(){
	//alert($("#department_id option:selected").text());
	//return;
		var url="index.php?r=akMaster_PengajarPelajaran/form&op=add&department_id=" + $("#department_id").val()+"&karyawan_id=" + $("#karyawan_id").val();
		var winName = "formTahunAjaran";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=450,height=300;
		var replace = false,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
	}
	
	function closeTest(){
		
	}
	
	function editRow(id){		
		var url="index.php?r=akMaster_PengajarPelajaran/form&op=edit&id=" + id;
		var winName = "";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=450,height=300;
		var replace = true,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
	}
	
	function deleteRow(id){
		if (confirm("Delete data ?")){			
			window.parent.showLoader();			
			var data = $("#tblData").jqGrid("getRowData",id);
			var editUrl = $("#tblData").jqGrid("getGridParam","editUrl");		
			alert(editUrl);	
			$.ajax({	
				url : editUrl,
				type:"GET",
				async:true,
				data: {op: "del",id:data.id},
				success: function(data) {
					if(data.actionStatus == 1){
						alert(data.actionMessage);
					}						
				},
				complete:function(){
					window.parent.hideLoader();
				}
			});			
			$("#tblData").jqGrid("delRowData",id);
		}
	}
	*/
	
	function updateRow(id,stat,karyawan){
		if (confirm("Update data ?")){			
			window.parent.showLoader();			
//			var data = $("#tblData").jqGrid("getRowData",id);
			var editUrl = $("#tblData").jqGrid("getGridParam","editUrl");
			//alert(stat);			
			$.ajax({	
				url : editUrl,
				type:"GET",
				async:true,
				data: {op: "update",id:id,stat:stat,karyawan:karyawan},
				success: function(data) {
					if(data.actionStatus == 1){
						alert(data.actionMessage);
					}						
				},
				complete:function(){
					window.parent.hideLoader();
				}
			});			
//			$("#tblData").jqGrid('setGridParam',{datatype:'json'}).trigger('reloadGrid');
		}
	}
		

	
	$(function(){
		var department_id =$("#department_id").val();		
		var karyawan_id =$("#karyawan_id").val();			
 			
		var numberTemplate = {formatter: 'number', align: 'right', sorttype: 'number'};	 
		$("#tblData").jqGrid({
			url:'index.php?r=akGuruDanPelajaran_PengajarPelajaran/detailList',
			editUrl:'index.php?r=akGuruDanPelajaran_PengajarPelajaran/edit',
			postData:{"department_id":department_id,"karyawan_id" :karyawan_id},
			datatype: 'json',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			colNames: ['id','Pelajaran','Singkatan', 'Keterangan', 'Status' ],
			colModel: [
				{name: 'id', index: 'id', width: 40,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'name', index: 'name', width: 40,search:true},
				{name: 'singkatan', index: 'singkatan'},
				{name: 'keterangan', index: 'keterangan'},
				{name: 'checkstat', index: 'checkstat', width: 60, align: 'center',
                         formatter: 'checkbox', editoptions: { value: '1:0' },
                         formatoptions: { disabled: false },
                     },	
				
			],
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'Pengajar Pelajaran',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
				
			},/*
			 onSelectRow: function(id,status){
					 var ch =  jQuery(this).find('#'+id+' input[type=checkbox]').prop('checked');
					 var kar=$("#karyawan_id").val();
					  var chx =  jQuery(this).find('#karyawan_id input[type=select]').val();
					if(ch) {
				//	  jQuery(this).find('#'+id+' input[type=checkbox]').prop('checked',false);alert(id)
						 updateRow(id,0,kar)
					} else {
				//		  jQuery(this).find('#'+id+' input[type=checkbox]').prop('checked',true);alert(id)                       
						 updateRow(id,1,kar)						  
					}
			 }, */
                beforeSelectRow: function (rowid, e) {
					var kar =  $('#karyawan_id').val();
                    var $target = $(e.target), $td = $target.closest("td"),
                        iCol = $.jgrid.getCellIndex($td[0]),
                        colModel = $(this).jqGrid("getGridParam", "colModel");
                    if (iCol >= 0 && $target.is(":checkbox")) {
								  if ($target.is(':checked')) {
						   updateRow(rowid,1,kar)
					} else { updateRow(rowid,0,kar)}
                       /* alert("checkbox is " +
                              ($target.is(":checked")? "checked" : "unchecked") +
                              " in the column \"" + colModel[iCol].name +
                              "\" in the row with rowid=\"" + rowid + "\"");
						*/	  
                    }
                    return true;
                },		 
			gridComplete: function(){ 
			/*
				var ids = jQuery("#tblData").getDataIDs(); 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i]; 
					//btnEdit = "<input style='height:22px;width:20px;' type='button' value='E' onclick=jQuery('#rowed2').editRow("+cl+"); ></ids>"; 
					//btnDelete = "<input style='height:20px;width:20px;' type='button' value='S' onclick=jQuery('#rowed2').saveRow("+cl+"); />"; 
					btnEdit = "<img title='edit' class='imgBtn' style='height:20px;width:20px;' src='images/action/Actions-Edit-icon.png' onclick='editRow(" + cl + ")' />"; 
					btnDelete = "<img title='delete' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Actions-Delete-icon.png' onclick='deleteRow(" + cl + ")' />"; 
					jQuery("#tblData").setRowData(ids[i],{act:btnEdit+btnDelete}) 
					
				} 
				*/
			}
		});
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});
		
		//add search text
		$("#tblData").jqGrid('addSearchText',["name","keterangan"]);
		
		
	});
</script>

<div style="">
	<script>
		$(function(){
			$("#department_id").change(function(){
				$("#karyawan_name").val('');
				$("#karyawan_id").val('0');
				var karyawan_id =$("#karyawan_id").val();
				var	department_id=$("#department_id").val();
				var	karyawan_name=$("#karyawan_name").val();						
				$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val(),"karyawan_id" :karyawan_id},datatype:"json"}).trigger("reloadGrid");
			});
		});	
	</script>
	<script>
	function popupResult(obj){
		console.log(obj);
		$("#karyawan_name").val(obj.name);
		$("#karyawan_id").val(obj.id);
		var karyawan_id =$("#karyawan_id").val();
		var	department_id=$("#department_id").val();
		var	karyawan_name=$("#karyawan_name").val();		
		$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val(),"karyawan_id" :$("#karyawan_id").val()},datatype:"json"}).trigger("reloadGrid");
	}
</script>	    
	
	<div>
		<?php
			$karyawan_name = Yii::app()->request->getParam("karyawan_name","");
			$karyawan_id = Yii::app()->request->getParam("karyawan_id","");			
			$rsDep = MDepartment::model()->findAll(array("condition"=>"is_active = TRUE","order"=>"id"));
			$arrOptions=array();
			foreach($rsDep as $rw){
				$selected = $department_id == $rw->id ? true : false;
				$arrOptions[] = array("label"=>$rw->name,"value"=>$rw->id,"selected"=>$selected);
			}
			$this->widget('application.components.widgets.SelectField', 
				array("name"=>"department_id",
					"value"=>"",
					"htmlOptions"=>"",
					"label"=>array(
						"value"=>"Department",
						"htmlOptions"=>"class ='mandatory' style='display:inline-block;width:100px'"
					),
					"options"=>$arrOptions,
				)
			);
			
		?>		
	</div>
<div>
    <?php 
		echo "<label class='form mandatory' style='width:100px;'>Pengajar</label>";
		$this->widget('application.components.widgets.FindTextField', 
			array('options' => array('id'=>'karyawan_name','value'=>$karyawan_name),
				'windowParams' => array('url'=>"index.php?r=akGuruDanPelajaran_JadwalPelajaran/popupWindow",'name' => 'popup_target', 
					'specs'=> array('height'=>500), 
				),
				'callback'=>"popupResult",
			)
		);			
    ?>		
    <input type="hidden" id="karyawan_id" name="karyawan_id" value="<?php echo $karyawan_id ?>"  />
    <input type="hidden" id="karyawan_name" name="karyawan_name" value="<?php echo $karyawan_name ?>"  />

</div>   	
       
	<div style="float:rigth">
	<?php 
		$this->widget('application.components.widgets.ActionBar', 
			array("menuAlign" => "right", 
				'items' => array(
					array(
						'id' => "btnRefresh",
						'title' => Yii::t("menu","Segarkan"),
						'icon' => "images/action/Action-Refresh-icon.png",
						'callback'=>"reloadPage"
					),					
				/*	array(
						'id' => "btnNew",
						'title' => Yii::t("menu","Baru"),
						'icon' => "images/action/Actions-document-new-icon.png",
						'callback'=>"newData"
					),*/
					array(
						'id' => "btnNew",
						'title' => Yii::t("menu","Cetak"),
						'icon' => "images/action/Actions-document-print-icon2.png",
						'callback'=>"printData"
					),					
					
				)
			)
		);
	?>
	</div>	
	<div style="clear:both"></div>	
	<table id="tblData"><tr></tr></table>
	<div id="navTblData"></div>
    
    
</div>